package yz;

import java.util.HashSet;
import java.util.Set;

/**
 * [83. 删除排序链表中的重复元素](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/)
 *
 * @author Yz
 * @since 2022-09-27 18:59
 */
public class LeetCode83_RemoveDuplicatesFromSortedList {
    public ListNode deleteDuplicates(ListNode head) {
        if (head == null) {
            return null;
        }
        Set<Integer> set = new HashSet<>();
        set.add(head.val);
        ListNode start = head;
        while (start.next != null) {
            if (set.contains(start.next.val)) {
                start.next = start.next.next;
            } else {
                set.add(start.next.val);
                start = start.next;
            }
        }
        return head;
    }
    
    static class ListNode {
        int val;
        ListNode next;
        
        public ListNode() {
        }
        
        public ListNode(int val) {
            this.val = val;
        }
        
        public ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
}
